package com.zeus.core.config.listener;

import com.zeus.common.log.ZeusLoggerConfig;
import com.zeus.core.context.ZeusContext;
import com.zeus.core.report.ZeusReportClient;
import com.zeus.core.report.ZeusReportServiceShutdownClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

/**
 * Spring容器初始化完成
 *
 * @author f1ybear_wang  2021/5/13 16:16
 */
@Slf4j
public class ZeusMonitorContextClosedEventListener implements ApplicationListener<ContextClosedEvent> {

    /**
     * Spring容器销毁处理事件
     *
     * @param event ApplicationContext刷新事件
     * @author f1ybear_wang  2021/5/13 17:52
     */
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        try {
            ZeusReportClient client = new ZeusReportServiceShutdownClient();
            if (ZeusContext.getContext().shutdownNotifyEnable()) {
                client.complete();
            }
        } catch (Throwable throwable) {
            if (log.isWarnEnabled() && ZeusLoggerConfig.needEnableLogger()) {
                log.warn("### Zeus监控服务, 发送服务关闭通知出现异常 >> errMsg = {}", throwable.getMessage());
            }
        }
    }


}
